﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfFlipit
{
    /// <summary>
    /// SettingWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SettingWindow : Window
    {
        private readonly FlipItSettings _settings;

        public SettingWindow()
        {
            InitializeComponent();
        }

        public SettingWindow(FlipItSettings settings) : this()
        {
            this._settings = settings;
            this.txtVersion.Text = $"版本: {GetVersion()}";
        }

        private string GetVersion()
        {
            var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
            return $"{version.Major}.{version.Minor}.{version.Build}";
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            this._settings.Display24HrTime = this.cb24Hour.IsChecked.Value;
            this._settings.ShowSeconds = this.cbShowSecond.IsChecked.Value;
            this._settings.FontFamily = this.cmbFontFamily.Text;
            this._settings.FontSize = this.cmbFontSize.Text;

            this._settings.Save();
            this.Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (this._settings.Display24HrTime)
            {
                this.cb24Hour.IsChecked = true;
            }
            else
            {
                this.cb12Hour.IsChecked = true;
            }

            this.cbShowSecond.IsChecked = this._settings.ShowSeconds;

            // 绑定字体数据源
            ICollection<FontFamily> fontList = Fonts.SystemFontFamilies;
            this.cmbFontFamily.ItemsSource = fontList.OrderBy(o => o.Source);
            if (!string.IsNullOrEmpty(this._settings.FontFamily))
            {
                this.cmbFontFamily.SelectedItem = new FontFamily(this._settings.FontFamily);
            }
            else
            {
                this.cmbFontFamily.SelectedIndex = 0;
            }

            List<double> fontSizeList = new List<double>() { 100, 90, 80, 70, 60, 50 };
            this.cmbFontSize.ItemsSource = fontSizeList;
            if (!string.IsNullOrEmpty(this._settings.FontSize))
            {
                this.cmbFontSize.SelectedItem = double.Parse(this._settings.FontSize);
            }
            else
            {
                this.cmbFontSize.SelectedIndex = 2;
            }
        }
    }
}
